<?php

namespace app\admin\model;

use app\admin\model\superdata\Activitycategory as cat;
use app\admin\model\vactivities\Activity;
use think\Db;
use think\Model;
use think\Session;

class Admin extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 重置用户密码
     * @author baiyouwen
     */
    public function resetPassword($uid, $NewPassword)
    {
        $passwd = $this->encryptPassword($NewPassword);
        $ret = $this->where(['id' => $uid])->update(['password' => $passwd]);
        return $ret;
    }

    // 密码加密
    protected function encryptPassword($password, $salt = '', $encrypt = 'md5')
    {
        return $encrypt($password . $salt);
    }

    public function getCatByUid($uid){

        if(empty($uid))
            return false;
        $map['status'] = Activity::ACTIVITY_STATUS_1;
        $res = Db::name('activity')->where('admin_id', $uid)->where($map)->group('category')->field('category')->select();

        if ($res) {
            foreach($res as $k=>$v){
                $list[] = self::getCatNameById($v['category']);
            }
            $list = array_filter($list);
            $list = array_values($list);
            unset($map);
            //print_r($list);
            return $list;
        } else {
            return false;
        }
    }
    /*
     * 统计发布的所有活动所属标签
     */
    public function getTagsByUid($uid){
        if(empty($uid))
            return false;
        $map['status'] = Activity::ACTIVITY_STATUS_1;
        $res = Db::name('activity')->where('admin_id', $uid)->where($map)->group('tags')->field('tags')->select();
        if ($res) {
            foreach($res as $k=>$v){
                $l[] = self::getTagNameById($v['tags']);
            }
            $l = array_filter($l);
            //print_r($l);die;
            $l = array_values($l);
            unset($map);
            return $l;
        } else {
            return false;
        }
    }

    /*
     * 通过分类id查找分类名称
     */
    static private function getCatNameById($id){
        if(empty($id))
            return false;
        $map['status'] = cat::ACTIVITY_STATUS_1;
        $res = Db::name('activity_category')->where('id', $id)->where($map)->field('name')->find();
        if($res)
            return $res['name'];
        else
            return '';
    }

    /*
      * 通过标签id查找标签名称
    */
    static private function getTagNameById($id){
        if(empty($id))
            return false;
        $map['status'] = cat::ACTIVITY_STATUS_1;
        $res = Db::name('activity_tags')->where('id', $id)->where($map)->field('name')->find();
        if($res)
            return $res['name'];
        else
            return '';
    }
}
